package com.winglish.as3.player 
{
	import com.winglish.as3.loader.SWFLoader;
	import com.winglish.as3.player.vo.ControlBar;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author charmjin
	 */
	public class SwfPlayer extends ControlBar
	{
		private var swf:SWFLoader;
		private var loadMc:MovieClip;
		private var autoPlay:Boolean;
		
		/**
		 * 자동 플레이, play버튼과 pause 버튼이 하나의 무비클립으로 사용이 되는지 설정.
		 * @param	autoPlay
		 * @param	sameBtn play 무비클립과 pause 무비클립이 하나의 무비클립으로 1,2번 프레임에 셋팅되어있다면 true.
		 */
		public function SwfPlayer(autoPlay:Boolean = false, sameBtn:Boolean = false ) 
		{
			autoPlay = autoPlay;
			sameBtnUse = sameBtn;
		}
		
		/**
		 * swf 로드.
		 * @param	str
		 */
		public function init(str:String):void {
			if(str){
				swf = new SWFLoader(str);
				swf.addEventListener(Event.INIT, swfLoadComplete);
			}
		}
		
		/**
		 * swf 로드 완료 이벤트.
		 * @param	e
		 */
		private function swfLoadComplete(e:Event):void {
			e.target.getContent().stop();
			dispatchEvent(new Event("swfLoad"));
		}
		
		/**
		 * 무비클립에 로드한 swf 매핑.
		 * @param	mc
		 */
		public function setLoadMc(mc:MovieClip):void {
			if (mc) {
				loadMc = mc;
				loadMc.addChild(swf.getContent());
				loadMc = swf.getContent();
				loadMc.addEventListener(Event.ENTER_FRAME, enterFrame);
				if (autoPlay) loadMc.play();
			}
		}
		
			
		/**
		 * 컨트롤바 볼 마우스 Up 이벤트.
		 * @param	e
		 */
		override protected function ballMouseUp(e:MouseEvent):void {
			super.ballMouseUp(e);
			if (loadMc) {
				if (loadMc.currentFrame != loadMc.totalFrames) {
					loadMc.play();
					if (sameBtnUse) {
					
					}else {
						if (playMc) playMc.gotoAndStop(2);
						if (pauseMc) pauseMc.gotoAndStop(1);
					}
				}else {
					loadMc.stop();
					if (sameBtnUse) {
					
					}else {
						if (playMc) playMc.gotoAndStop(1);
						if (pauseMc) pauseMc.gotoAndStop(2);
					}
				}
			}else {
				throw new Error( "로드 무비클립이 없습니다." );
			}
			
		}
		
		/**
		 * 컨트롤바 볼 마우스 move 이벤트
		 * @param	e
		 */
		override protected function ballMouseMove(e:MouseEvent):void {
			super.ballMouseMove(e);
			var no:int = Math.floor((ballMc.x / barMc.width) * loadMc.totalFrames);
			loadMc.gotoAndStop(no);
			e.updateAfterEvent();
		}
		
		/**
		 * play 클릭 이벤트.
		 * @param	e
		 */
		override protected function playMcClick(e:MouseEvent):void {
			loadMc.play();
			super.playMcClick(e);
		}
		
		/**
		 * pause 클릭 이벤트.
		 * @param	e
		 */
		override protected function pauseMcClick(e:MouseEvent):void {
			loadMc.stop();
			super.pauseMcClick(e);
		}
		
		/**
		 * replay 클릭 이벤트.
		 * @param	e
		 */
		override protected function replayBtnClick(e:MouseEvent):void {
			loadMc.gotoAndPlay(1);
			super.replayBtnClick(e);
		}
		
		/**
		 * stop 클릭 이벤트.
		 * @param	e
		 */
		override protected function stopMcClick(e:MouseEvent):void {
			loadMc.gotoAndStop(1);
			super.stopMcClick(e);
		}
	
		
		/**
		 * 컨트롤바 이동 EnterFrame 이벤트.
		 * @param	e
		 */
		override protected function enterFrame(e:Event):void {
			if (!isDrag) {
				if (ballMc) {
					ballMc.x = (loadMc.currentFrame / loadMc.totalFrames) * barMc.width;
					stateMc.width = ballMc.x;
				}
				
				if (loadMc.currentFrame == loadMc.totalFrames) {
					loadMc.stop();
					if (sameBtnUse) {
					
					}else {
						if (playMc) playMc.gotoAndStop(1);
						if (pauseMc) pauseMc.gotoAndStop(2);
					}
				}
			}
		}
	}
}